﻿using ShopManagement.Model.EF;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace ShopManagement
{
    internal class ShopServices
    {//获得商品数据库
        public List<Products> GetAll()
        {
            var db = new Shop2024Entities();
            return db.Products.ToList();
        }
        //添加商品
        public void Create(ProductsCreateModel model)
        {
            var db = new Shop2024Entities();
            var newItem = new Products()
            {
                
                Name = model.Name,
                Description = model.Description,
                Price = model.Price,
                Stock = model.Stock,
                Category = model.Category,
            };
            db.Products.Add(newItem);
            db.SaveChanges();
        }
        //修改商品
        public Products GetById(int id)
        {
            var db = new Shop2024Entities();
            return db.Products.Find(id);
        }
        public void Edit(ProductsEditModel model)
        {
            var db = new Shop2024Entities();
            var item = db.Products.Find(model.ProductID);
            if (item != null)
            {
                item.Name = model.Name;
                item.Description = model.Description;

                item.Price = model.Price;
                item.Stock = model.Stock;
                item.Category = model.Category;
            }
            db.SaveChanges();
        }
        //删除商品
        public void Delete(int id)
        {
            var db = new Shop2024Entities();
            var item = db.Products.Find(id);
            if (item != null)
            {
                db.Products.Remove(item);
                db.SaveChanges();
            }
        }
    }
}
